package com.chen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.bean.constants.WxConstants;
import com.chen.bean.entity.DiaLogEntity;
import com.chen.bean.entity.MessageEntity;
import com.chen.bean.vo.DialogVo;
import com.chen.bean.vo.MessageVo;
import com.chen.dao.DiaLogMapper;
import com.chen.service.DialogService;
import com.chen.service.MessageService;
import com.chen.util.CacheUtils;
import com.chen.util.JsonMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DialogServiceImpl implements DialogService {

    @Autowired
    private DiaLogMapper diaLogMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private CacheUtils cacheUtils;

    @Override
    public void update(DiaLogEntity entity) {
        diaLogMapper.updateById(entity);
    }

    @Override
    public void save(String dialogNumber, String dialogName,Integer userId) {
        DiaLogEntity entity = new DiaLogEntity();
        entity.setDialogNumber(dialogNumber);
        entity.setDialogName(dialogName);
        entity.setUserId(userId);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        diaLogMapper.insert(entity);
    }

    @Override
    public List<DialogVo> queryHistoryDialogs(Integer userId) {
        LambdaQueryWrapper<DiaLogEntity> lqw = new LambdaQueryWrapper<DiaLogEntity>();
        lqw.eq(DiaLogEntity::getUserId,userId).orderByAsc(DiaLogEntity::getCreateTime);
        List<DiaLogEntity> diaLogEntities = diaLogMapper.selectList(lqw);
        return BeanUtil.copyToList(diaLogEntities, DialogVo.class);
    }

    @Override
    public List<MessageVo> queryByDialogNumber(String dialogNumber, Integer userId) {
        List<MessageEntity> messageEntities;
        String key = WxConstants.WX_HISTORY_MESSAGE_CACHE + dialogNumber;
        String json = JsonMapper.toJson(cacheUtils.getCacheList(key));
        messageEntities = JsonMapper.formJson(json, new TypeReference<List<MessageEntity>>() {
        });
        //缓存为空查询数据库
        if (CollectionUtil.isEmpty(messageEntities)){
            messageEntities =  messageService.queryByDialogNumber(dialogNumber);
            if (CollectionUtil.isNotEmpty(messageEntities)){
                cacheUtils.setCacheList(key,messageEntities);
            }else {
                return new ArrayList<>();
            }
        }
        return BeanUtil.copyToList(messageEntities, MessageVo.class);
    }

    @Override
    public DiaLogEntity queryEntityByDialogNumber(String dialogNumber) {
        LambdaQueryWrapper<DiaLogEntity> lqw = new LambdaQueryWrapper<DiaLogEntity>();
        lqw.eq(DiaLogEntity::getDialogNumber,dialogNumber);
        return diaLogMapper.selectOne(lqw);
    }
}
